package com.molichuxing.gateway.bff.dealer.services.impl;

import com.molichuxing.framework.exception.BizException;
import com.molichuxing.gateway.bff.dealer.convertor.Convert;
import com.molichuxing.gateway.bff.dealer.entity.request.create.FenceCreateVo;
import com.molichuxing.gateway.bff.dealer.entity.request.modify.FenceModifyVo;
import com.molichuxing.gateway.bff.dealer.entity.response.FenceVo;
import com.molichuxing.gateway.bff.dealer.services.FenceBffService;
import com.molichuxing.services.infrastructure.dto.request.create.FenceCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.FenceModifyDto;
import com.molichuxing.services.infrastructure.dto.response.FenceStoreStockDto;
import com.molichuxing.services.infrastructure.dto.response.IovFenceDto;
import com.molichuxing.services.infrastructure.dto.response.StoreDto;
import com.molichuxing.services.infrastructure.service.FenceStoreStockService;
import com.molichuxing.services.infrastructure.service.IovFenceService;
import com.molichuxing.services.infrastructure.service.StoreService;
import com.molichuxing.services.property.FenceStoreStockTypeEnum;
import com.molichuxing.services.property.StoreStatusEnum;
import com.molichuxing.services.property.StoreVerifyStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 围栏相关bff
 *
 * @author zhaopenghui
 * @date 2020年02月24日
 */
@Service("fenceBffService")
public class FenceBffServiceImpl implements FenceBffService {
    private static final Logger logger = LoggerFactory.getLogger(FenceBffServiceImpl.class);

    @Resource
    private FenceStoreStockService fenceStoreStockService;
    @Resource
    IovFenceService iovFenceService;
    @Resource
    StoreService storeService;

    /**
     * 围栏已有列表
     *
     * @param storeId
     * @param type
     * @return
     */
    @Override
    public List<FenceVo> getBindingFenceList(Integer storeId, Integer type) {
        if (FenceStoreStockTypeEnum.STORE.getValue() == type) {
            checkStoreStatus(storeId);
        }

        FenceStoreStockTypeEnum fenceStoreStockType = FenceStoreStockTypeEnum.getEnum(type);
        List<Integer> fenceIdList = fenceStoreStockService.getFenceByStoreId(storeId, fenceStoreStockType);
        if (null == fenceIdList || fenceIdList.size() < 1) {
            return null;
        }
        List<IovFenceDto> fenceList = iovFenceService.getFenceList(fenceIdList);
        return Convert.toFenceListVo(fenceList);
    }

    /**
     * 围栏可添加列表
     *
     * @param storeId
     * @param type
     * @return
     */
    @Override
    public List<FenceVo> getUnBindingFenceList(Integer storeId, Integer type) {
        if (FenceStoreStockTypeEnum.STORE.getValue() == type) {
            checkStoreStatus(storeId);
        }

        List<IovFenceDto> fenceList = iovFenceService.getFenceList(null);
        if (null == fenceList || fenceList.size() < 1) {
            return null;
        }

        List<Integer> fenceIdList = fenceStoreStockService.getAllFenceIdList();
        List<IovFenceDto> fenceResultList = new ArrayList<>();
        for (IovFenceDto iovFenceDto : fenceList) {
            Integer fenceId = iovFenceDto.getId();
            if (!fenceIdList.contains(fenceId)) {
                fenceResultList.add(iovFenceDto);
            }
        }

        return Convert.toFenceListVo(fenceResultList);
    }

    /**
     * 解除围栏
     *
     * @param fenceModifyVo
     * @return
     */
    @Override
    public Boolean deleteFence(FenceModifyVo fenceModifyVo) {
        Integer type = fenceModifyVo.getType();
        Integer storeId = fenceModifyVo.getStoreId();
        if (FenceStoreStockTypeEnum.STORE.getValue() == type) {
            checkStoreStatus(storeId);
        }

        Integer fenceId = fenceModifyVo.getFenceId();
        Boolean bindingVehicleBoolean = iovFenceService.getBindingVehicleByFenceId(fenceId);
        if (bindingVehicleBoolean) {
            throw new BizException("存在绑定车辆，请重新选择围栏");
        }

        FenceModifyDto fenceModifyDto = Convert.toFenceModifyDto(fenceModifyVo);
        return fenceStoreStockService.deleteFence(fenceModifyDto);
    }

    /**
     * 添加围栏
     *
     * @param fenceCreateVo
     * @return
     */
    @Override
    public Boolean createFence(FenceCreateVo fenceCreateVo) throws Exception{
        Integer type = fenceCreateVo.getType();
        Integer storeId = fenceCreateVo.getStoreId();
        List<Integer> fenceIdList = fenceCreateVo.getFenceIdList();
        if (FenceStoreStockTypeEnum.STORE.getValue() == type) {
            checkStoreStatus(storeId);
        }

        List<IovFenceDto> fenceList = iovFenceService.getFenceList(fenceIdList);
        if (null == fenceList || fenceList.size() < 1) {
            throw new BizException("围栏不存在，请重新选择");
        }

        List<FenceStoreStockDto> fenceStoreStockDtoList = fenceStoreStockService.getFenceByFenceIdList(fenceIdList);
        if (fenceStoreStockDtoList != null && fenceStoreStockDtoList.size() > 0) {
            FenceStoreStockDto fenceStoreStockDto = fenceStoreStockDtoList.get(0);
            Integer fenceId = fenceStoreStockDto.getFenceId();
            Map<Integer, IovFenceDto> fenceMap = new HashMap<Integer, IovFenceDto>();
            if (fenceList != null && fenceList.size() > 0) {
                fenceMap = fenceList.stream().collect(Collectors.toMap(IovFenceDto::getId, a -> a, (k1, k2) -> k1));
            }

            IovFenceDto iovFenceDto = fenceMap.get(fenceId);
            if (iovFenceDto != null) {
                throw new BizException(iovFenceDto.getFenceName() + "已被绑定，请重新选择围栏");
            } else {
                throw new BizException("围栏已被绑定，请重新选择围栏");
            }
        }

        FenceCreateDto fenceCreateDto = Convert.toFenceCreateDto(fenceCreateVo);
        return fenceStoreStockService.createFence(fenceCreateDto);
    }


    /**
     * 门店校验
     *
     * @param storeId
     * @return
     */
    private Boolean checkStoreStatus(Integer storeId) {
        StoreDto storeDto = null;
        try {
            storeDto = storeService.getStoreById(storeId);
        } catch (Exception e) {
            logger.error("获取门店信息异常", e);
            throw new BizException("获取门店信息异常");
        }

        if (null == storeDto) {
            throw new BizException("门店不存在");
        }

        Integer verifyStatus = storeDto.getVerifyStatus();
        Integer status = storeDto.getStatus();
        if (StoreVerifyStatusEnum.VERIFY_PASS.getValue() == verifyStatus) {
            if (StoreStatusEnum.OPEN.getValue() == status || StoreStatusEnum.SUSPENSION.getValue() == status) {
                return true;
            } else {
                throw new BizException("门店状态不正确，不能进行操作");
            }
        } else {
            throw new BizException("门店" + StoreVerifyStatusEnum.getName(verifyStatus) + "，不能进行操作");
        }
    }


}
